<?php

class Web_Model_DbTable_Resource extends Zend_Db_Table_Abstract
{
    protected $_name = 'resource';

    public function getAll($where) {

        $sql = $this->getAdapter()->select()
                ->from($this->_name)                
                ->order('category');

        if (is_array($where)) {
            foreach($where as $key=>$value) {
                $sql->where($key, $value);
            }
        }
        
        return $this->_db->fetchAll($sql);
    }
    
    public function getResourceInformation($resID) {
        $sql = $this->getAdapter()->select()
                ->from('resource_meta')
                ->where('resID = ?', $resID);
        
        return $this->_db->fetchRow($sql);
    }


    public function getCategory() {
        $sql = $this->getAdapter()->select()
                ->from('category', array('catID', 'name'));
        
        return $this->_db->fetchAll($sql);
    }
    
    public function getResAndCategory() {
        $sql = $this->getAdapter()->select()
                ->from('category');
        
        $cat = $this->_db->fetchAll($sql);
        
        $data = array();
        foreach($cat as $value) {        
            $sql2 = $this->getAdapter()->select()
                    ->from(array('a'=>$this->_name))
                    ->joinLeft(array('b'=>'category'), 'a.category = b.catID', array('name as family'))
                    ->where('category = ?', $value['catID']);
            
            $family = $value['name'];
            $resource = $this->_db->fetchAll($sql2);
            
            $data[] = array('family'=>$family, 'resource'=>$resource);
        }
        
        return $data;
    }
}